package com.wl.dzt.basic.entity;

import com.wl.dzt.sys.annotation.Column;
import com.wl.dzt.sys.annotation.Table;
import com.wl.dzt.sys.core.dao.DB;
import com.wl.dzt.sys.core.dao.select.term.Term;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@ApiModel("BasicNumGenerate")
@Table("basic_num_generate")
public class BasicNumGenerate implements Serializable {

    @ApiModelProperty(value = "单号生成器说明", name = "numCreateReson")
    @Column(value = "num_create_reson", text = "单号生成器说明", maxLength = 32767)
    private String numCreateReson;


    @ApiModelProperty(value = "当前编号码", name = "currentCode")
    @Column(value = "current_code", text = "当前编号码", maxLength = 150)
    private String currentCode;


    @ApiModelProperty(value = "编号键", name = "numKey")
    @Column(value = "num_key", text = "编号键", maxLength = 100)
    private String numKey;


    @ApiModelProperty(value = "编号", name = "id")
    @Column(value = "id", text = "编号", maxLength = 32)
    private String id;


    @ApiModelProperty(value = "前缀代码", name = "prefixCode")
    @Column(value = "prefix_code", text = "前缀代码", maxLength = 10)
    private String prefixCode;


    @ApiModelProperty(value = "后缀代码", name = "suffixCode")
    @Column(value = "suffix_code", text = "后缀代码", maxLength = 10)
    private String suffixCode;


    @ApiModelProperty(value = "位数", name = "digit")
    @Column(value = "digit", text = "位数", maxLength = 5)
    private Integer digit;

    public String getNumCreateReson() {
        return numCreateReson;
    }

    public void setNumCreateReson(String numCreateReson) {
        this.numCreateReson = numCreateReson;
    }

    public String getCurrentCode() {
        return currentCode;
    }

    public void setCurrentCode(String currentCode) {
        this.currentCode = currentCode;
    }

    public String getNumKey() {
        return numKey;
    }

    public void setNumKey(String numKey) {
        this.numKey = numKey;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPrefixCode() {
        return prefixCode;
    }

    public void setPrefixCode(String prefixCode) {
        this.prefixCode = prefixCode;
    }

    public String getSuffixCode() {
        return suffixCode;
    }

    public void setSuffixCode(String suffixCode) {
        this.suffixCode = suffixCode;
    }

    public Integer getDigit() {
        return digit;
    }

    public void setDigit(Integer digit) {
        this.digit = digit;
    }

    /**
     * <b>获取单据生成器</b>

     *
     * @param key
     *            单号键
     * @return 单号字符串
     */
    public static synchronized String getNumCreate(String key) {
        BasicNumGenerate numCreate = DB.get(BasicNumGenerate.class, new Term[] { Term.eq("numKey", key) });
        if (numCreate != null) {
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(numCreate.getCurrentCode());
            Long code = Long.valueOf(m.replaceAll("").trim()) + 1;
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < numCreate.getDigit() - code.toString().length(); i++) {
                builder.append("0");
            }
            String nowCode = numCreate.getPrefixCode() + builder.toString() + code + numCreate.getSuffixCode();
            numCreate.setCurrentCode(nowCode);
            DB.save(numCreate);
            return nowCode;
        }
        return "";
    }

}

